/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RenderAnimFrame.java
 *
 * Created on 02-Dec-2010, 02:53:28
 */

package camp.anim;

import camp.jme.Preview;
import campskeleton.CampSkeleton;
import campskeleton.Plan;
import campskeleton.PlanSkeleton;
import java.io.File;
import javax.swing.JOptionPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import straightskeleton.ui.NaiveMould;
import utils.SimpleFileChooser;

/**
 *
 * @author twak
 */
public class RenderAnimFrame extends javax.swing.JFrame {

    /** Creates new form RenderAnimFrame */
    public RenderAnimFrame()
    {
        initComponents();

        startFrame.setModel( new SpinnerNumberModel( 100, 0, Integer.MAX_VALUE, 1) );
        endFrame.setModel( new SpinnerNumberModel( 100, 0, Integer.MAX_VALUE, 1) );

        pack();
        setLocationRelativeTo( null );
        setVisible( true );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dirField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        browseButton = new javax.swing.JButton();
        meshItem = new javax.swing.JCheckBox();
        endFrame = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        goButton = new javax.swing.JButton();
        startFrame = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("output dir:");

        browseButton.setText("browse");
        browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButtonActionPerformed(evt);
            }
        });

        meshItem.setText("meshes");
        meshItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                meshItemActionPerformed(evt);
            }
        });

        jLabel2.setText("start frame:");

        goButton.setText("go");
        goButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("end frame:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2))
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(dirField, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                    .addComponent(meshItem))
                                .addGap(30, 30, 30)
                                .addComponent(browseButton))
                            .addComponent(startFrame, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
                            .addComponent(endFrame, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)))
                    .addComponent(goButton, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dirField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(browseButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(startFrame, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(endFrame, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(meshItem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(goButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_browseButtonActionPerformed
    {//GEN-HEADEREND:event_browseButtonActionPerformed
        new SimpleFileChooser( CampSkeleton.instance, false, "select a file in directory to use ;)" )
        {
            @Override
            public void heresTheFile(File f) throws Throwable
            {
                if (!f.isDirectory())
                {
                    f = f.getParentFile();
                }
                dirField.setText( f.getAbsolutePath() );
            }
        };
    }//GEN-LAST:event_browseButtonActionPerformed

    private void meshItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_meshItemActionPerformed
    {//GEN-HEADEREND:event_meshItemActionPerformed
        // nothing to do!
    }//GEN-LAST:event_meshItemActionPerformed

    private void goButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_goButtonActionPerformed
    {//GEN-HEADEREND:event_goButtonActionPerformed
        final int 
                end = ((Integer) endFrame.getValue()),
                start = ((Integer) startFrame.getValue());

        new Thread()
        {

            public void run()
            {
                CampSkeleton.instance.update( start );
                Plan plan = CampSkeleton.instance.plan;
                PlanSkeleton s = new PlanSkeleton( plan );
                s.skeleton();

                for ( int i = start; i < end; i++ )
                    try
                    {
                        CampSkeleton.instance.update( i, false );
                        plan = CampSkeleton.instance.plan;
                        s = new PlanSkeleton( plan );
                        s.skeleton();

                        if ( s.output.faces != null )
                        {
                            Preview preview = CampSkeleton.instance.preview;

                            preview.clear = true;
                            preview.threadKey = s;


                    preview.display( s.output, true, true, false );

                    System.out.println("1) now dumping "+i);
//                    preview.takeScreenShot(String.format ( fileField.getText() + "%04d.png", i) );

//                    if ( meshCheck.isSelected() )
//                        s.addMeshesTo( preview );

                    do {
                        Thread.sleep( 100 );
                    }
                    while (preview.isPendingUpdate());

                    preview.dump( new File( String.format( dirField.getText() + "/m1/%04d.obj", i ) ) );

                    preview.clear = true;
                    preview.threadKey = s;

                    preview.display( s.output, false, false, true );

                    System.out.println("2) now dumping "+i);
//                    preview.takeScreenShot(String.format ( fileField.getText() + "%04d.png", i) );
                    do {
                    Thread.sleep( 100 );
                    }
                    while (preview.isPendingUpdate());

//                    if ( meshCheck.isSelected() )
//                        s.addMeshesTo( preview );

                    preview.dump( new File( String.format( dirField.getText() + "/m2/%04d.obj", i ) ) );



//                            preview.display( s.output, true, true );
//
//                            if ( meshItem.isSelected() )
//                                s.addMeshesTo( preview );
//
////                            while (preview.isPendingUpdate())
//                                Thread.sleep( 1000 );

                            // wait for preview to display on next frame
//                            while ( preview.isPendingUpdate() )
//                                Thread.sleep( 1000 );

//                            preview.dump( new File( String.format( dirField.getText() + "%04d.obj", i ) ) );
//                    Thread.sleep( 1000 );
                        }
                    } catch ( Exception ex )
                    {
                        JOptionPane.showMessageDialog( RenderAnimFrame.this, "error saving file :(" );
                        ex.printStackTrace();
                        return;
                    }
                // flush changes from preview

                CampSkeleton.instance.update( 0 );
                CampSkeleton.instance.somethingChanged();
                goButton.setText( "go" );
            }
        }.start();

    }//GEN-LAST:event_goButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RenderAnimFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseButton;
    private javax.swing.JTextField dirField;
    private javax.swing.JSpinner endFrame;
    private javax.swing.JButton goButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JCheckBox meshItem;
    private javax.swing.JSpinner startFrame;
    // End of variables declaration//GEN-END:variables

}
